﻿Imports System.Windows.Forms
Imports DotSpatial.Data, DotSpatial.Symbology

Public Class frmClip

    Private Sub frmCreateRaster_Load(sender As Object, e As EventArgs) Handles Me.Load

        Try

            Dim GridLayerFound As Boolean = False
            With cboSourceLayer.Items
                .Clear()
                For Each lyr In GISForm.GISMap.GetRasterLayers
                    If Not String.IsNullOrEmpty(lyr.LegendText) Then .Add(lyr.LegendText)
                Next
                For Each lyr In GISForm.GISMap.GetPolygonLayers
                    If Not String.IsNullOrEmpty(lyr.LegendText) Then .Add(lyr.LegendText)
                    If lyr.LegendText = WRDBGridLayerName Then GridLayerFound = True
                Next
            End With
            If GridLayerFound Then
                cboSourceLayer.Text = WRDBGridLayerName
            Else
                If cboSourceLayer.Items.Count > 0 Then cboSourceLayer.SelectedIndex = 0
            End If

            With cboPolyLayer.Items
                .Clear()
                For Each lyr In GISForm.GISMap.GetPolygonLayers
                    If Not String.IsNullOrEmpty(lyr.LegendText) AndAlso lyr.LegendText <> WRDBGridLayerName Then .Add(lyr.LegendText)
                Next
            End With
            If cboPolyLayer.Items.Count > 0 Then cboPolyLayer.SelectedIndex = 0

            GetControlValues(REGAPP, Me)

        Catch ex As Exception

            ErrorMsg(, ex)

        End Try

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Try

            If cboSourceLayer.SelectedIndex = -1 OrElse cboPolyLayer.SelectedIndex = -1 Then Exit Sub

            Dim lyrClip As FeatureLayer = GISForm.GetLayer(cboPolyLayer.Text)
            Dim lyrSource As Layer = GISForm.GetLayer(cboSourceLayer.Text)
            If TypeOf lyrClip Is FeatureLayer Then
                clsGrid.ClipWithPolygon(lyrSource, lyrClip)
            Else
                clsRaster.ClipWithPolygon(lyrSource, lyrClip)
            End If

            If TypeOf lyrClip IsNot FeatureLayer Then
                'after changing values, cannot figure out how to refresh the raster; need to remove and reload the raster and reapply the color scheme
                GISForm.GISMap.Layers.Remove(lyrSource)
                GISForm.GISMap.AddLayer(CType(lyrSource, RasterLayer).Filename)
            End If

            GISForm.GISMap.Invalidate()
            GISForm.Refresh()
            GISForm.GISLegend.Refresh()
            SaveControlValues(REGAPP, Me)

            If WRDBProject IsNot Nothing AndAlso GISForm.WRDBGridLayer Is lyrSource AndAlso GISForm.WRDBGrid IsNot Nothing Then
                With GISForm.WRDBGrid
                    .ComputeWeights()
                    .UpdateGrid()
                    .CreateScheme()
                End With
            End If

            If WRDBProject IsNot Nothing AndAlso chkResetProjectClippingLayer.Visible AndAlso chkResetProjectClippingLayer.Checked Then
                WRDBProject.GisClippingLayerFilename = CType(GISForm.GetLayer(cboPolyLayer.Text), PolygonLayer).DataSet.Filename
                WRDBProject.SavePreferences()
            End If

            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()

        Catch ex As Exception

            ErrorMsg(, ex)

        End Try

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub cboSourceLayer_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSourceLayer.SelectedIndexChanged
        chkResetProjectClippingLayer.Visible = (WRDBProject IsNot Nothing AndAlso cboSourceLayer.Text = WRDBGridLayerName)
    End Sub
End Class
